package main

import (
	"html/template"
	"net/http"
)

type ContractDetails struct {
	Email   string
	Subject string
	Message string
}

type ResponseData struct {
	Success  bool
	Contract ContractDetails
}

func main() {
	tmpl := template.Must(template.ParseFiles("forms.html"))
	http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
		//get 请求 ,返回html
		if request.Method != http.MethodPost {
			tmpl.Execute(writer, nil)
		} else {
			//post请求，准备接收数据
			details := ContractDetails{
				Email:   request.FormValue("email"),
				Subject: request.FormValue("subject"),
				Message: request.FormValue("message"),
			}
			respData := ResponseData{
				Success:  true,
				Contract: details,
			}
			tmpl.Execute(writer, respData)
		}
	})
	http.ListenAndServe(":8989", nil)
}
